package com.woniuxy.oa.dao;

import com.woniuxy.oa.dao.provider.EmpProvider;
import com.woniuxy.oa.entity.Emp;
import com.woniuxy.oa.entity.Permission;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author:Sun yue wang
 * @date:2019/12/25
 * @description:用户表的DAO层
 */
public interface EmpDAO {
    /**
     * 增加用户
     *
     * @param emp
     */
    @Insert("insert into oa_emp values(#{id},#{empNo},#{name},#{password},#{phoneNum},#{birthDate},#{email},#{dept.id},#{gender},#{avatar},#{level},#{entryTime},#{lastTime},#{lastIp},#{departureTime},#{status})")
    public void add(Emp emp);

    /**
     * 通过用户id改变用户状态
     *
     * @param id
     * @param status
     */
    @Update("update oa_emp set status=#{status} where id=#{id}")
    public void updateUserStatusById(@Param("id") String id, @Param("status") String status);

    /**
     * 修改用户信息通过用户id
     *
     * @param emp
     */
    @Update("update oa_emp set name=#{name}, password=#{password}, phonenum=#{phoneNum}, birthdate=#{birthDate}, email=#{email}, deptid=#{dept.id}, gender=#{gender}, avatar=#{avatar}, level=#{level}, entrytime=#{entryTime}, lasttime=#{lastTime}, lastip=#{lastIp}, departuretime=#{departureTime}, status=#{status} where id=#{id}")
    public void updateEmpById(Emp emp);

    /**
     * 多条件查询用户
     *
     * @param emp
     */
    @SelectProvider(type = EmpProvider.class, method = "getAllEmpByCondition")
    public List<Emp> getAllEmp(Emp emp);

    /**
     * 通过员工id查询员工信息
     *
     * @param id
     * @return
     */
    @Select("select e.* ,d.id, d.deptname from oa_emp e,oa_dept d where e.deptid=d.id and e.id=#{id}")
    @Results({
            @Result(id = true, column = "deptid", property = "dept.id"),
            @Result(column = "deptname", property = "dept.deptName")
    })
    public Emp getEmpById(String id);

    /**
     * 通过姓名密码查找员工
     *
     * @param name
     * @param password
     * @return
     */
    @Select("select * from oa_emp where name=#{name} and password=#{password}")
    public Emp getEmpByNameAndPwd(@Param("name") String name, @Param("password") String password);

    /**
     * 通过员工id和密码修改密码
     *
     * @param
     * @param password
     * @param newPassword
     */
    @Update("update oa_emp set password=#{newPassword} where id=#{id} and password=#{password}")
    public void updatePwd(@Param("id") String id, @Param("password") String password, @Param("newPassword") String newPassword);

    /**
     * 通过id删除用户
     *
     * @param status
     * @param id
     */
    @Update("update oa_emp set status=#{status} where id=#{id}")
    public void delById(String status, String id);

    /**
     * @param id
     * @return
     */
    @Select("select p.* from oa_emp e,oa_permission p,oa_emp_perm ep where e.`id`=ep.`empid` and ep.`permid`=p.`id` and e.`id`=#{id}")
    public List<Permission> getPerm(String id);
    @Select("select * from oa_emp where deptid=#{deptid}")
    public List<Emp> getEmpsByDeptId(String deptid);

    /**
     * @Description //查询所有员工
     * @Date 2019/12/27 17:04
     * @param
     *@return java.util.List<com.woniuxy.oa.entity.Emp>
     **/
    @Select("select * from oa_emp")
    public List<Emp> getEmpAll();
}
